package com.jiayinlending.jiayindai.activity.mine;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiayinlending.jiayindai.R;
import com.jiayinlending.jiayindai.activity.base.BaseActivity;
import com.jiayinlending.jiayindai.widget.CustomPopupWindow;

import butterknife.Bind;


public class MobileAuthenticationActivity extends BaseActivity {
    private CustomPopupWindow mCustomPopupWindow;
    private Button bt_dismiss;//悬浮窗的关闭按钮
    private View mLayoutPopupWindowView;//悬浮窗的布局
    @Bind(R.id.ll_mobile)
    LinearLayout llMobile;
    private EditText et_qqqqq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getToolbarTitle().setText("手机认证");
        getSubTitle().setText("");

        //打开服务密码提醒
        showPupAlert();
        //打开验证码
        showPupVerification();
    }
    private void showPupVerification() {
        Button btShowpup = (Button) findViewById(R.id.bt_showpup);
        btShowpup.setOnClickListener(new View.OnClickListener() {

            private TextView tv_verification;
            private Button bt_sure;

            @Override
            public void onClick(View v) {
                LayoutInflater inflater=MobileAuthenticationActivity.this.getLayoutInflater();
                View verification = inflater.inflate(R.layout.popupwindow_activity_code, null);
                et_qqqqq = (EditText)verification.findViewById(R.id.et_qqqqq);
                bt_sure = (Button) verification.findViewById(R.id.bt_sure);
                tv_verification = (TextView) verification.findViewById(R.id.tv_verification);
                final AlertDialog ad=new AlertDialog.Builder(MobileAuthenticationActivity.this).create();
                ad.setView(verification,0,0,0,0);
                ad.show();
                ad.getWindow().setDimAmount(0.9f);
                        ad.setCanceledOnTouchOutside(false);
                bt_sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (tv_verification.getText().equals("请再次输入验证码")){
                            ad.dismiss();
                        }else{
                            tv_verification.setText("请再次输入验证码");
                            et_qqqqq.setText("");
                            et_qqqqq.setHint("请再次输入验证码");
                        }
                    }
                });
            }
        });
    }

    private void showPupAlert() {
        ImageView ivShowpup = (ImageView) findViewById(R.id.iv_showpup);
        ivShowpup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow(R.layout.popupwindow_activity_mobile, R.id.ll_mobile, R.id.bt_bottom);

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mobile_authentication;
    }


    /**
     * 显示悬浮窗，并设置背景透明度
     */
    public void showPopupWindow(int resource, int id, final int btn) {
        mLayoutPopupWindowView = LayoutInflater.from(this).inflate(resource, null);
        mCustomPopupWindow = new CustomPopupWindow(findViewById(id),
                this, mLayoutPopupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout
                .LayoutParams.WRAP_CONTENT, true);
        mCustomPopupWindow.setOnPopupWindowListener(new CustomPopupWindow
                .PopupWindowListener() {

            // TODO 设置活动内容
            @Override
            public void initView() {
                bt_dismiss = (Button) mLayoutPopupWindowView.findViewById(btn);
                bt_dismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCustomPopupWindow.dismiss();
                        mCustomPopupWindow.setBackgroundAlpha(1);
                    }
                });
            }
        });
        mCustomPopupWindow.showView();
        mCustomPopupWindow.setBackgroundAlpha(0.1f);
    }

}
